<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: zzh
  Date: 2022/4/20
  Time: 11:52 AM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>更改首页文字</title>
</head>
<body>
<div>
    <center>
        <div>
            <div><h2>${title}</h2></div>
            <div>
                <table>
                    <tr>
                        <th>当前文字:</th>
                        <th><p>${welcomeText}</p></th>
                    </tr>
                </table>
            </div>
            <div>
                <button onclick="change()">修改文字</button>
            </div>
            <div style="display: none" id="changeDiv">
                <div>
                    <div>
                        <table>
                            <tr>
                                <th><h6>修改文字</h6></th>
                            </tr>
                            <tr>
                                <th><textarea id="changeText"></textarea></th>
                            </tr>
                        </table>
                    </div>
                    <div>
                        <button onclick="changeTrue()">确认修改</button>
                    </div>
                </div>
            </div>
        </div>
    </center>
</div>
</body>
<script type="text/javascript" src='<c:url value="/static/js/jquery.js" />'></script>
<script type="text/javascript">
    function change() {
        let elementById = document.getElementById("changeDiv");
        if (elementById.style.display === "block") {
            elementById.style.display = "none";
        } else {
            elementById.style.display = "block"
        }
    }

    function changeTrue() {
        let changeText = document.getElementById("changeText").value;
        if (changeText.length > 0) {
            if (confirm("确认修改首页文字?")) {
                var settings = {
                    "url": "${url}" + "/api/welcome/change?text=" + changeText,
                    "method": "GET",
                    "timeout": 0,
                    "headers": {
                        "token": sessionStorage.getItem("token")
                    },
                };
                $.ajax(settings).done(function (response) {
                    if (response.status === 200) {
                        history.go(0)
                    } else {
                        alert(response.data.error)
                    }
                });
            }
        }
    }
</script>
</html>
